package scribes.mybus.ejb;

import java.io.StringWriter;
import java.util.Date;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class KMLOutput implements OutputFormatInterface {

	/**
	 * 
	 * method : parseObject
	 * returns : Kml output
	 * 
	 */
	// member variables
	public int busId;
	public String busName;
	public int capacity;	
	private Date entryDateTime;
	private double stationLatitude;
	private double stationLongitude;
	private String location;
	private	String routeStatus;
	private String message;
	private int driverId;
	private String operationCode;
	public String type;
	
	@Override
	   

		public String parseObject(List<BusInfoOutput> busData) {

			KMLOutput KML = new KMLOutput();
			try {
					DocumentBuilderFactory docfactory = DocumentBuilderFactory.newInstance();
					DocumentBuilder docbuilder = docfactory.newDocumentBuilder();
					TransformerFactory tranFactory = TransformerFactory.newInstance();
					Transformer aTransformer = tranFactory.newTransformer();
					Document doc = docbuilder.newDocument();
					//creating root node
					Element rootElement = doc.createElement("kml");
					rootElement.setAttribute("xmlns", "http://earth.google.com/kml/2.1");
					doc.appendChild(rootElement);
					//creating child nodes
					Element dnode = doc.createElement("Document");
					rootElement.appendChild(dnode);
					
					Element elestyle = doc.createElement("Style");
					elestyle.setAttribute("id", "busStyle");
					
					Element iconstyle = doc.createElement("IconStyle");
					iconstyle.setAttribute("id", "busIcon");				
					Element eleicon = doc.createElement("Icon");
					Element busiconhref = doc.createElement("href");
					busiconhref.appendChild(doc.createTextNode("http://maps.google.com/mapfiles/kml/shapes/bus.png"));
					elestyle.appendChild(iconstyle);
					eleicon.appendChild(busiconhref);
					iconstyle.appendChild(eleicon);
					dnode.appendChild(elestyle);
						
					for(int i=0; i<busData.size(); i++)
					{
						//KML.busId =((BusInfoOutput) busData).getId();
						//KML.busName = ((BusInfoOutput) busData).getStation_name();
					//	KML.entryDateTime = busData.getEntry_datetime();
						//KML.stationLatitude = ((BusInfoOutput) busData).getStation_latitude();
						//KML.stationLongitude = ((BusInfoOutput) busData).getStation_longitude();
						KML.capacity = ((BusInfoOutput) busData).getPassengerCount();
						//KML.driverId =((BusInfoOutput) busData).getDriverId();
						KML.routeStatus = ((BusInfoOutput) busData).getRouteStatus();
						KML.operationCode = ((BusInfoOutput) busData).getOperationCode();
						KML.location = ((BusInfoOutput) busData).getLocation();
						//KML.message = ((BusInfoOutput) busData).getMessage();
					
						Element placemark = doc.createElement("Placemark");
						dnode.appendChild(placemark);
						Element name = doc.createElement("name");
						name.appendChild(doc.createTextNode(KML.busName));	
						placemark.appendChild(name);
						
						Element styleUrl = doc.createElement("styleUrl");
			            styleUrl.appendChild(doc.createTextNode("#busStyle"));
						
			            Element point = doc.createElement("Point");
		                Element coordinate = doc.createElement("coordinates");
		                coordinate.appendChild(doc.createTextNode("-121.97243,37.39079"));
		                point.appendChild(coordinate);
			            	            
						Element descrip = doc.createElement("description");
						descrip.appendChild(doc
								.createTextNode("Bus Name :"	+ (KML.busName) + "\n"));
						descrip.appendChild(doc
								.createTextNode("\nstationLatitude: " + (KML.stationLatitude)));
						descrip.appendChild(doc
								.createTextNode("\nstationLongitude: " + (KML.stationLongitude)));
						descrip.appendChild(doc
								.createTextNode("\nPassenger Count : " + (KML.capacity)));
						descrip.appendChild(doc
								.createTextNode("\nLocation: " + (KML.location)));
						descrip.appendChild(doc
								.createTextNode("\nRoute Status: " + (KML.routeStatus)));
						descrip.appendChild(doc
								.createTextNode("\nOperation Code: " + (KML.operationCode)));
						descrip.appendChild(doc
								.createTextNode("\nDriver Id: " + (KML.driverId)));
						
						placemark.appendChild(name);
		                placemark.appendChild(styleUrl);
		                placemark.appendChild(point);
		             	placemark.appendChild(descrip);
		             	dnode.appendChild(placemark);
	             	
	             	}
					
					//this element draws a path
	             	Element Placemark = doc.createElement("Placemark");
	                Element LineString = doc.createElement("LineString");
	                Element extrude = doc.createElement("extrude");
	                extrude.appendChild(doc.createTextNode("0"));
	                Element tessellate = doc.createElement("tessellate");
	                tessellate.appendChild(doc.createTextNode("1"));
	                Element coordinate1 = doc.createElement("coordinates");
	                coordinate1.appendChild(doc.createTextNode("0")); 
	                LineString.appendChild(extrude);
	                LineString.appendChild(tessellate);
	                LineString.appendChild(coordinate1);
	                Placemark.appendChild(LineString);

	                dnode.appendChild(Placemark);
	             	
					Source src = new DOMSource(doc);
					
					StringWriter kmlWriter = new StringWriter();
					Result dest = new StreamResult(kmlWriter);
					
					aTransformer.transform(src, dest);
					System.out.println("KML generation completed.....");
					return kmlWriter.toString();
			} catch (Exception e) {
				System.out.println(e.getMessage());
			}
			return "";

}}
	